package com.example.activiti.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.activiti.engine.*;
import org.activiti.spring.ProcessEngineFactoryBean;
import org.activiti.spring.SpringProcessEngineConfiguration;
import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;

/**
 * @author PengQiang
 * @date 2019-04-27 23:26
 */
@Configuration
public class ActivitiConfig {


    @Autowired
    private DataSource dataSource;

    @Bean
    public ObjectMapper objectMapper(){
        return new ObjectMapper();
    }

    @Bean
    public SpringProcessEngineConfiguration springProcessEngineConfiguration(){
        SpringProcessEngineConfiguration configuration = new SpringProcessEngineConfiguration();
        configuration.setDataSource(dataSource);
        configuration.setTransactionManager(transactionManager());
        configuration.setJobExecutorActivate(false);
        configuration.setDatabaseSchemaUpdate("true");
        configuration.setProcessDefinitionCacheLimit(10);
        configuration.setActivityFontName("宋体");
        configuration.setLabelFontName("宋体");
        return configuration;
    }


    @Bean
    public PlatformTransactionManager transactionManager(){
        PlatformTransactionManager manager = new DataSourceTransactionManager(dataSource);
        return manager;
    }

    @Bean
    public ProcessEngineFactoryBean processEngineFactoryBean(){
        ProcessEngineFactoryBean factoryBean = new ProcessEngineFactoryBean();
        factoryBean.setProcessEngineConfiguration(springProcessEngineConfiguration());
        return factoryBean;
    }

    @Bean
    public RuntimeService runtimeService() throws Exception{
        return processEngineFactoryBean().getObject().getRuntimeService();
    }

    @Bean
    public TaskService taskService() throws Exception{
        return processEngineFactoryBean().getObject().getTaskService();
    }

    @Bean
    public FormService formService() throws Exception{
        return processEngineFactoryBean().getObject().getFormService();
    }
    @Bean
    public HistoryService historyService() throws Exception{
        return processEngineFactoryBean().getObject().getHistoryService();
    }

    @Bean
    public IdentityService identityService() throws Exception{
        return processEngineFactoryBean().getObject().getIdentityService();
    }

    @Bean
    public ManagementService managementService() throws Exception{
        return processEngineFactoryBean().getObject().getManagementService();
    }

    @Bean
    public RepositoryService repositoryService() throws Exception {
        return processEngineFactoryBean().getObject().getRepositoryService();
    }
}
